using System;
using System.Collections.Generic;
using System.Text;
using dal = Microsoft.ConnectionEngine.Registers.Core.ServiceProvider.DataAccess;
using dao = Microsoft.ConnectionEngine.Registers.Core.ServiceProvider.Common;
using Microsoft.ConnectionEngine.Common;

using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration;


namespace Microsoft.ConnectionEngine.Registers.Core.ServiceProvider.Business
{
	/// <summary>
	/// Provides Business Logic for Message operations
	/// </summary>
    public class MessageManager
	{
        private static dao.ReferenceData refData;

        //Work Item ID: 126
        //Modified By: SachinJo
        //Unused code block removed/commented.
        //private static void Initialize()
        //{

        //}

        /// <summary>
        /// Validate a Connection Engine Message based on information provided by the message. A message is valid if:
        /// the Source and Destination Service providers belong to the same pool within Service Provider Register and
        /// the message can be sent from the Source Service Provider Type to the Destination Service Provider Type.
        /// </summary>
        /// <param name="msg">Represents the Connection Engine Message to be validated</param>
        /// <returns>
        /// Returns the original Connection Engine Message, with an updated status element indicating if the message is valid or not.
        /// If the message is valid, the Receiver.URI parameter is also updated.
        /// </returns>
        public static ConnectionEngineMessage ValidateConnectionEngineMessage(ConnectionEngineMessage msg)
        {
            ConnectionEngineMessage result = msg;
            if (dal.ServiceProvider.ServiceProviderInSamePool(msg.Header.Sender.OrganisationID, msg.Header.Receiver.OrganisationID) > 0)
            {
                if (dal.Message.MessageTypeValidateBySourceDestinationProvider(
                                        ((int)msg.Header.InteractionType).ToString(),
                                        msg.Header.Sender.OrganisationID,
                                        msg.Header.Receiver.OrganisationID) > 0)
                {
                    result.Header.Status.Code = (int)(SystemAdapterExceptionCause.Ok);
                    result.Header.Status.Description = SystemAdapterException.GetExceptionDescription(SystemAdapterExceptionCause.Ok);
                    result.Header.Receiver.URI = Business.ServiceProviderManager.GetServiceProviderByID(msg.Header.Receiver.OrganisationID).WebServiceURL;
                }
                else
                {
                    throw new SystemAdapterException(SystemAdapterExceptionCause.MessageHasNotValidDestination, null, null);
                }
            }
            else
            {
                throw new SystemAdapterException(SystemAdapterExceptionCause.SourceAndDestinationNotInTheSamePool, null, null);
            }
            return result;
        }

        /// <summary>
        /// Validates if a Connection Engine Message payload is valid according to its message type.
        /// </summary>
        /// <param name="payload">Represents the payload being validated.</param>
        /// <param name="messagetypeid">Represents the message type assigned to the payload.</param>
        /// <returns>Returns a boolean indicating if the payload is valid or not.</returns>
        [Obsolete("Use System Adapter Payload validation method instead)")]
        public static bool ValidatePayload(string payload, int messagetypeid)
        {
            string message = "";
            if (refData == null)
            {
                refData = RefDataManager.GetRefData();
            }

            dao.MessageType currentMsgType = null;
            foreach (dao.MessageType msgtype in refData.MessageTypes)
            {
                if (msgtype.MessageTypeID == messagetypeid)
                    currentMsgType = msgtype;
            }
            if (currentMsgType != null)
                return XmlValidator.ValidateXml(payload, ref message, currentMsgType.XmlSchema);
            else
                return false;
        }

    }
}
